/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.agent.service;

import cn.ac.iie.agent.tools.XmlTools;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author zhangyun
 */
public class XmlService {

    private static Logger logger = Logger.getLogger(XmlService.class);

    public boolean check(String xml, Map<String, String> rsMap) {
        boolean paraFlag = true;
        String status = "sucess";
        String desc = "";
        //得到任务号
        String subjobid = XmlTools.getElValueFromStr(xml, "subjobid");
        if (subjobid.equals("")) {
            status = "fail";
            desc = "subjobid is empty!";
            rsMap.put("status", status);
            rsMap.put("desc", desc);
            logger.info("subjobid is empty");
            paraFlag = false;
        } else {
            //rsMap.put("subjobid", subjobid);
        }
        //得到本机ip
        String agentIP = XmlTools.getElValueFromStr(xml, "agentIPOne");
        if (agentIP.equals("")) {
            status = "fail";
            desc = "agentIP is empty!";
            rsMap.put("subjobid", subjobid);
            rsMap.put("status", status);
            rsMap.put("desc", desc);
            logger.info(subjobid + " agentIP is not a long!");
            paraFlag = false;
        } else {
            //rsMap.put("agentIP", agentIP);
        }
        //得到hdfsPath
        String hdfsPath = XmlTools.getElValueFromStr(xml, "hdfsPathOne");
        if (hdfsPath.equals("")) {
            status = "fail";
            desc = "hdfsPathOne is empty!";
            rsMap.put("subjobid", subjobid);
            rsMap.put("status", status);
            rsMap.put("desc", desc);
            logger.info(subjobid + " hdfsPathOne is not a long!");
            paraFlag = false;
        } else {
            //rsMap.put("hdfsPathOne", hdfsPath);
        }
        //得到超时时间
        long timeout;
        String timeouttmp = XmlTools.getValueFromStrDGText(xml, "timeout");
        if (timeouttmp.equals("")) {
            status = "fail";
            desc = subjobid + " timeout is empty!";
            rsMap.put("subjobid", subjobid);
            rsMap.put("status", status);
            rsMap.put("desc", desc);
            paraFlag = false;
            logger.info(subjobid + " timeout is empty!");
        } else {
            //rsMap.put("timeout", timeouttmp);
        }
        try {
            timeout = Long.parseLong(timeouttmp);
        } catch (Exception ex) {
            status = "fail";
            desc = subjobid + " timeout is not a long!";
            rsMap.put("subjobid", subjobid);
            rsMap.put("status", status);
            rsMap.put("desc", desc);
            paraFlag = false;
            logger.info(subjobid + " timeout is not a long!", ex);
        }
        //得到系统名称
        String name = XmlTools.getValueFromStrDGText(xml, "name");
        logger.info(subjobid + " name is " + name);
        if (name.equals("")) {
            status = "fail";
            desc = subjobid + " name is empty!";
            rsMap.put("subjobid", subjobid);
            rsMap.put("status", status);
            rsMap.put("desc", desc);
            paraFlag = false;
            logger.info(subjobid + " name is empty!");
        } else {
            //rsMap.put("name", name);
        }
        //得到采集类型
        String type = XmlTools.getValueFromStrDGText(xml, "type");
        logger.info(subjobid + " type is " + type);
        if (type.equals("")) {
            status = "fail";
            desc = subjobid + " type is empty!";
            rsMap.put("subjobid", subjobid);
            rsMap.put("status", status);
            rsMap.put("desc", desc);
            paraFlag = false;
            logger.info(subjobid + " type is empty!");
        } else {
            //rsMap.put("type", type);
        }
        return paraFlag;
    }
}
